const {
    chromium
} = require('playwright');
const path = require('path');
const fs = require('fs');
const util = require('util');
const FileType = require('file-type');

// 获取所有图片

const pic_path = "E:/旧资料/备份/F/来自陆/图片/我的相机";
const i_want_to_say = "一个平凡的相册";
const last_at = " #相册# #照片# #2013# #2014# #2015# [纸飞机]";
const pic_path_split = 9;

const page_number = " %s/%s";
const time_wait = 20;


(async () => {

    let file_all = []
    let image_count = 0
    fs.readdirSync(pic_path).forEach(async file => {
        file_all.push(file)
    });
    let image_all = [],
        split_images = []
    for (let file of file_all) {
        try {
            let filetype = await FileType.fromFile(path.resolve(pic_path, file))
            if (filetype.mime.startsWith("image")) {
                split_images.push(path.resolve(pic_path, file))
                image_count += 1
            }
            
        } catch (error) {
            console.log(error)
        }
        if (split_images.length === pic_path_split) {
            image_all.push(split_images)
            split_images = []
        }
    }
    if (split_images.length > 0) {
        image_all.push(split_images)
    }
    console.log(image_all)
    // for (let i in image_all){
    //     let out_string = i_want_to_say + util.format(page_number,i*pic_path_split + image_all[i].length,image_count)
    //     console.log(out_string)
    // }
    console.log('image init')

    // 开始发微博
    const browser = await chromium.launchPersistentContext(path.resolve(process.cwd(), `data/chromium-data-dir`), {
        args: ['--disk-cache-size=8073741824'], // in bytes = 2Gb
        headless: false,
        slowMo: 1000
    });
    const context = browser
    await context.addInitScript({
        path: './stealth.min.js'
    });

    //     //   const context = await browser.newContext();
    const page = await context.newPage();

    //     //   await page.goto('http://httpbin.org/headers');
    //     //   await page.goto('https://bot.sannysoft.com/');
    await page.goto('https://www.weibo.com/u/2476557765/home', {
        waitUntil: "load",
        timeout:90000
    });

    await page.waitForTimeout(10000);
    //   await page.pause();
    for (let i in image_all) {
        let out_string = i_want_to_say + util.format(page_number, i * pic_path_split + image_all[i].length, image_count) + "\n" + last_at
        console.log(out_string)
        // 输入对话框
        await page.fill('textarea', out_string,{timeout:90000});
        await page.waitForTimeout(1000);

        await page.click(".kind input[name='pic1']", {
            force: true,
            timeout:90000
        })
        console.log("click input file")
        await page.waitForTimeout(1500);
        await page.setInputFiles(".kind input[name='pic1']", image_all[i], {
            timeout:180000
        });
        console.log("wait upload file")
        await page.waitForTimeout(1000);
        await page.waitForSelector(".UI_scrollContent li.loading",{state:"detached",timeout:180000})
        console.log("upload success")
        await page.waitForTimeout(1000);
        await page.click(':nth-match(a:has-text("发布"), 2)', {
            force: true
        },{timeout:90000});

        await page.waitForTimeout(time_wait * 1000)

    }
    await page.pause()
    //   await page.waitForTimeout(1000000);


})();